<?php
/* $hodnotyy = array('IN', 'D', 'D0.5', 'LV', 'LV0.5', 'PN', 'DO', 'OT', 'OT0.5', 'MD', 'MOD', 'A');
  $hodnoty = array('IN', 'D', 'D0.5', 'LV', 'LV0.5', 'PN', 'DO', 'OT', 'OT0.5', 'MD', 'MOD', 'A');
  $hodnoty2 = array('P', 'D', 'DP', 'LV', 'LVP', 'PN', 'DO', 'OT','OTP', 'MD',  'MOD', 'A');
  $hodnoty3 = array('V práci', 'Dovolenka', 'Pol dňa - dovolenka', 'Lekárske vyšetrenie', 'Pol dňa - lekárske vyštrenie', 'PN', 'Deň voľna', 'Voľno za nadčas', 'Pol dňa - voľno za nadčas', 'Materská dovolenka', 'Manager on duty', 'Absencia');
  $farby = array('#fff', '#92bdf8', '#92bdf8', '#d5f892', '#d5f892', '#f8cc92', '#fcfbbe', '#f998ff', '#f998ff', '#c2faf0', '#fc2a39', 'red');
  $farbyTlac = array('#000', 'blue', 'blue', 'green', 'green', 'maroon', 'orange', 'purple', 'purple', 'navy', 'red', 'red');
 */

$hodnotyy = array('IN', 'D', 'LV', 'PN', 'DO', 'OT', 'MD', 'MOD', 'A', 'LD');
$hodnoty = array('IN', 'D', 'LV', 'PN', 'DO', 'OT', 'MD', 'MOD', 'A', 'LD');
$hodnoty2 = array('P', 'D', 'LV', 'PN', 'DO', 'OT', 'MD', 'MOD', 'A', 'LD');
$hodnoty3 = array('V práci', 'Dovolenka', 'Lekárske vyšetrenie', 'PN', 'Deň voľna', 'Voľno za nadčas', 'Materská dovolenka', 'Manager on duty', 'Absencia', 'Lekárske vyšetrenie - doprovod');
$farby = array('#fff', '#92bdf8', '#d5f892', '#f8cc92', '#fcfbbe', '#f998ff', '#c2faf0', '#fc2a39', 'red', '#d5f892');
$farbyTlac = array('#000', 'blue', 'green', 'maroon', 'orange', 'purple', 'navy', 'red', 'red', 'green');

$about_user_s = "SELECT spolid FROM user WHERE id = '" . $user_id . "' ";
$about_user_q = MySQL_Query($about_user_s);
$user = MySQL_Fetch_Array($about_user_q);
//echo $user['spolid'];

if (isset($_SESSION['bmb_spolid'])) {
    $user['spolid'] = $_SESSION['bmb_spolid'];
}
//echo $user['spolid'];

$dni = array('ned', 'pon', 'uto', 'str', 'štv', 'pia', 'sob');
$mesiace = array('', 'Január', 'Február', 'Marec', 'Apríl', 'Máj', 'Jún', 'Júl', 'August', 'September', 'Október', 'November', 'December');

$dnes = date("U", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
$pocetTyzdnov = ($dnes - $bodNula) / (7 * 24 * 60 * 60);
$pocetTyzdnov = (int) $pocetTyzdnov;
$pocetTyzdnov++;
$aktualnyTyzden = $pocetTyzdnov;
$pocetTyzdnov += 10;
if (isset($_POST['mesiac'])) {
    $mesiac = $_POST['mesiac'];
} else {
    $mesiac = date("n:Y");
}
echo '<form style="margin: 10px 0; border-bottom: 1px solid gray; padding-bottom: 2px;" action="' . $_SERVER['REQUEST_URI'] . '" method="post" id="kalendar">';
$formID = "'kalendar'";
echo '<select name="mesiac" size="1" onchange="submitForm(' . $formID . ')">';
$temp = '';
for ($i = $pocetTyzdnov; $i > 0; $i--) {
    if ($temp != date("M-Y", ($bodNula + ($i - 1) * (7 * 24 * 60 * 60)))) {
        if (date("n:Y", ($bodNula + ($i - 1) * (7 * 24 * 60 * 60))) == $mesiac)
            echo '<option selected value="' . date("n:Y", ($bodNula + ($i - 1) * (7 * 24 * 60 * 60))) . '">' . date("M-Y", ($bodNula + ($i - 1) * (7 * 24 * 60 * 60))) . '</option>';
        else
            echo '<option value="' . date("n:Y", ($bodNula + ($i - 1) * (7 * 24 * 60 * 60))) . '">' . date("M-Y", ($bodNula + ($i - 1) * (7 * 24 * 60 * 60))) . '</option>';
        $temp = date("M-Y", ($bodNula + ($i - 1) * (7 * 24 * 60 * 60)));
    }
}
echo '</select>';
echo '&nbsp;&nbsp;<span style="padding: 1px 5px; background: yellow">Sviatok</span>';
echo '&nbsp;&nbsp;<span style="padding: 1px 5px; background: green">Manažér</span>';
echo '&nbsp;&nbsp;<span style="padding: 1px 5px; background: #74d8d8">Dohodár</span>';
echo '&nbsp;&nbsp;* Nočných za mesiac, Sviatkov za mesiac, Nadčas za rok = počet celých odpracovaných hodín';
echo '</form>';

$datum = explode(":", $mesiac);
$pocet_dni = date("t", mktime(0, 0, 0, $datum[0], 1, $datum[1]));

$posOtaz = strpos($_SERVER['REQUEST_URI'], '?');
$burl = substr($_SERVER['REQUEST_URI'], $posOtaz + 1);
?>

<form style="margin-top: 20px; float: left" action="vystupnaToExcel.php?mod=excel&amp;burl=<?php echo $burl ?>" method="post">
    <input name="zero" type="hidden" value="<?php echo $bodNula ?>" />
    <input name="user" type="hidden" value="<?php echo $user_id ?>" />
    <input name="month" type="hidden" value="<?php echo $mesiac ?>" />
    <input name="" value="Exportovať do Excelu" type="submit" />
</form>

<form style="margin-top: 20px; float: left;margin-left: 20px" action="vystupnaPrint.php?mod=print&amp;burl=<?php echo $burl ?>" method="post">
    <input name="zero" type="hidden" value="<?php echo $bodNula ?>" />
    <input name="user" type="hidden" value="<?php echo $user_id ?>" />
    <input name="month" type="hidden" value="<?php echo $mesiac ?>" />
    <input name="" value="Vytlačiť" type="submit" />
</form>

<?php
$about_userr_s = "SELECT spolid FROM user WHERE id = '" . $user_id . "' ";
$about_userr_q = MySQL_Query($about_userr_s);
$userr = MySQL_Fetch_Array($about_userr_q);
if ($userr['spolid'] == 0) {
    ?>
    <form style="margin-top: 20px; float: left;margin-left: 20px" action="" method="post">
        <input onclick="exportXML('<?php echo $mesiac ?>',<?php echo $user['spolid'] ?>)" value="Exportovať XML neprítomností" type="button" />
    </form>

    <form style="margin-top: 20px; float: left;margin-left: 20px" action="" method="post" id="vypisXml">
        <?php
        $d = explode(':',$mesiac);
        $xml_s = "SELECT * FROM xmls WHERE cid='" . $_SESSION['bmb_spolid'] . "' AND month='" . $d[0] . "' AND year='" . $d[1] . "'";
        $xml_q = MySQL_Query($xml_s);
        $xml_n = MySQL_Num_Rows($xml_q);
        if ($xml_n == 1) {
            $xml = MySQL_Fetch_Array($xml_q);
            echo 'Export neprítomnosti: <a href="get_fileXml.php?cid=' . $_SESSION['bmb_spolid'] . '&amp;m=' . $d[0] . '&amp;y=' . $d[1] . '">' . $xml['file'] . '.xml (' . $xml['date'] . ')</a>';
        }
        ?>
    </form>
    <?php
}
?>

<div style="height: 20px;clear: both"></div>

<table style="border-collapse: collapse; font-size: 13px; text-align: center; vertical-align: middle; width: 1500px">
    <tr>
        <td style="background: #cecece;border: 1px solid black;height: 20px; width: 200px;"><b><?php echo $mesiace[date("n", mktime(0, 0, 0, $datum[0], 1, $datum[1]))] . " " . date("Y", mktime(0, 0, 0, $datum[0], 1, $datum[1])) ?></b></td>
        <td style="background: #cecece;border: 1px solid black;height: 20px; width: 100px;" rowspan="2"><b>Strava za <?php echo date("n/Y", mktime(0, 0, 0, $datum[0], 1, $datum[1])) ?></b></td>
        <td style="background: #cecece;border: 1px solid black;height: 20px; width: 100px;" rowspan="2"><b>Mod k <?php echo date("n/Y", mktime(0, 0, 0, $datum[0], 1, $datum[1])) ?></b></td>
        <td style="background: #cecece;border: 1px solid black;height: 20px; width: 100px;" rowspan="2"><b>Nočné za <?php echo date("n/Y", mktime(0, 0, 0, $datum[0], 1, $datum[1])) ?></b></td>
        <td style="background: #cecece;border: 1px solid black;height: 20px; width: 100px;" rowspan="2"><b>Sviatky za <?php echo date("n/Y", mktime(0, 0, 0, $datum[0], 1, $datum[1])) ?></b></td>
        <td style="background: #cecece;border: 1px solid black;height: 20px; width: 100px;" rowspan="2"><b>Odrobené (dohodári) za <?php echo date("n/Y", mktime(0, 0, 0, $datum[0], 1, $datum[1])) ?></b></td>
        <?php
        for ($i = 1; $i < $pocet_dni + 1; $i++) {
            if (jeSviatok(date("U", mktime(0, 0, 0, $datum[0], $i, $datum[1])))) {
                echo '<td rowspan="2" style="background: yellow;height: 20px; width: 30px; border: 1px solid black;"><b>' . $i . '</b></td>';
            } else {
                echo '<td rowspan="2" style="background: #eeeeee;height: 20px; width: 30px; border: 1px solid black;"><b>' . $i . '</b></td>';
            }
        }
        ?>
    </tr>
    <tr>
        <td style="background: #cecece;border: 1px solid black;height: 20px; width: 200px;"><b>Meno</b></td>
    </tr>
    <?php
    $zam_s = "SELECT * FROM z WHERE spolid='" . $user['spolid'] . "' ORDER BY priezvisko ASC";
    $zam_q = MySQL_Query($zam_s);
    while ($zam = MySQL_Fetch_Array($zam_q)) {
        $odchod = explode('-', $zam['datum_odchodu']);
        $prichod = explode('-', $zam['datum_prichodu']);
        if ((((date("U", mktime(0, 0, 0, $odchod[1], $odchod[2], $odchod[0]))) >= (date("U", mktime(0, 0, 0, $datum[0], 1, $datum[1])))) || ($zam['datum_odchodu'] == '0000-00-00')) && (date("U", mktime(0, 0, 0, $prichod[1], $prichod[2], $prichod[0])) < date("U", mktime(0, 0, 0, $datum[0] + 1, 1, $datum[1])))) {
            ?>
            <tr>
                <?php
                if ($zam['manager'] == 1) {
                    ?>
                    <td style="background: green;border: 1px solid black;height: 20px; width: 200px;"><?php echo $zam['osobneCislo'] ?> <?php echo $zam['meno'] ?> <?php echo $zam['priezvisko'] ?> </td>
                    <?php
                } elseif ($zam['manager'] == 0) {
                    ?>
                    <td style="background: white;border: 1px solid black;height: 20px; width: 200px;"><?php echo $zam['osobneCislo'] ?> <?php echo $zam['priezvisko'] ?> </td>
                    <?php
                } else {
                    ?>
                    <td style="background: #74d8d8;border: 1px solid black;height: 20px; width: 200px;"><?php echo $zam['osobneCislo'] ?> <?php echo $zam['priezvisko'] ?> </td>
                    <?php
                }
                ?>
                <td style="background: #eee;border: 1px solid black;height: 20px; ">
                    <?php
                    $vypis = 0;
                    $nac_s = "SELECT SUM(kolko) as kolko FROM falkstrava WHERE zid = '" . $zam['id'] . "' AND mesiac='" . $datum[0] . "' AND rok='" . $datum[1] . "'";
                    $nac_q = MySQL_Query($nac_s);
                    $nac = MySQL_Fetch_Array($nac_q);
                    $vypis += $nac['kolko'];
                    echo '<input type="text" size="8" value="' . $vypis . '" onchange="upravStravuVystupna(' . $zam['id'] . ',' . $datum[0] . ',' . $datum[1] . ',this.value)"';
                    ?>
                </td>
                <td style="background: #eee;border: 1px solid black;height: 20px; ">
                    <?php
                    if ($zam['manager'] != 2) {
                        $vypis = 0;
                        if ($zam['manager'] == 1) {
                            $sum = vypocitajFinalnyModKMesiacu($zam['id'], $datum[0], $datum[1]);
                            $vypis += $sum;
                        }
                        echo $vypis;
                    } else {
                        echo '-';
                    }
                    ?>
                </td>
                <td style="background: #eee;border: 1px solid black;height: 20px; ">
                    <?php
                    if ($zam['manager'] != 2) {
                        $vypis = 0;
                        if ($zam['manager'] != 1) {
                            $nac_s = "SELECT SUM(kolko) as kolko FROM falknoc WHERE zid = '" . $zam['id'] . "' AND mesiac='" . $datum[0] . "' AND rok='" . $datum[1] . "'";
                            $nac_q = MySQL_Query($nac_s);
                            $nac = MySQL_Fetch_Array($nac_q);
                            $vypis += $nac['kolko'];
                            $vypis = $vypis / 3600;
                            $vypis = (int) $vypis;
                        }
                        echo $vypis;
                    } else {
                        echo '-';
                    }
                    ?>
                </td>
                <td style="background: #eee;border: 1px solid black;height: 20px; ">
                    <?php
                    if ($zam['manager'] != 2) {
                        $vypis = 0;
                        if ($zam['manager'] != 1) {
                            $nac_s = "SELECT SUM(kolko) as kolko FROM falksviatok WHERE zid = '" . $zam['id'] . "' AND mesiac='" . $datum[0] . "' AND rok='" . $datum[1] . "'";
                            $nac_q = MySQL_Query($nac_s);
                            $nac = MySQL_Fetch_Array($nac_q);
                            $vypis += $nac['kolko'];
                            $vypis = $vypis / 3600;
                            $vypis = (int) $vypis;
                        }
                        echo $vypis;
                    } else {
                        echo '-';
                    }
                    ?>
                </td>
                <td style="background: #eee;border: 1px solid black;height: 20px; ">
                    <?php
                    if ($zam['manager'] == 2) {
                        echo pocetOdpracovanychHodin($zam['id'], $datum[0], $datum[1]);
                    } else {
                        echo '-';
                    }
                    ?>
                </td>


                <?php
                $prichodDen = date("U", mktime(0, 0, 0, $prichod[1], $prichod[2], $prichod[0]));
                if ($prichodDen > date("U", mktime(0, 0, 0, $datum[0], 1, $datum[1]))) {
                    $pracuje = false;
                } else {
                    $pracuje = true;
                }
                $odchodDen = date("U", mktime(0, 0, 0, $odchod[1], $odchod[2], $odchod[0]));
                for ($i = 1; $i < $pocet_dni + 1; $i++) {
                    if ($prichodDen == date("U", mktime(0, 0, 0, $datum[0], $i, $datum[1]))) {
                        $pracuje = true;
                    }
                    if ($odchodDen == date("U", mktime(0, 0, 0, $datum[0], $i - 1, $datum[1]))) {
                        $pracuje = false;
                    }
                    if (($zam['datum_odchodu'] == '0000-00-00') || ($pracuje)) {
                        if ((date("w", mktime(0, 0, 0, $datum[0], $i, $datum[1])) == 0) || (date("w", mktime(0, 0, 0, $datum[0], $i, $datum[1])) == 6)) {
                            $farba = '#cecece';
                            $border = '1px solid black';
                        } elseif (jeSviatok(date("U", mktime(0, 0, 0, $datum[0], $i, $datum[1])))) {
                            $farba = 'yellow';
                            $border = '1px solid yellow';
                        } else {
                            $farba = 'white';
                            $border = '1px solid black';
                        }
                        $nac_s = "SELECT * FROM dochadzkazamfalk WHERE date='" . (mktime(0, 0, 0, $datum[0], $i, $datum[1])) . "' AND zid='" . $zam['id'] . "' ";
                        $nac_q = MySQL_Query($nac_s);
                        $nac = MySQL_Fetch_Array($nac_q);
                        if (isset($nac['id'])) {
                            if ((($nac['type'] == 'P') && (($nac['realOd'] != '') && ($nac['realDo'] != ''))) || ($nac['type'] == 'IN')) {
                                $hodn = 'IN';
                                //$farba = $farby[array_search('P', $hodnoty2)];
                            } elseif (($nac['type'] == 'OUT') || ((($nac['type'] == 'P') && (($nac['realOd'] == '') || ($nac['realDo'] == ''))))) {
                                //$farba = $farby[array_search('OUT', $hodnoty2)];
                                $hodn = '-';
                            } elseif ($nac['type'] == 'DP') {
                                $farba = $farby[array_search($nac['type'], $hodnoty2)];
                                $hodn = 'D0.5';
                            } elseif ($nac['type'] == 'LVP') {
                                $farba = $farby[array_search($nac['type'], $hodnoty2)];
                                $hodn = 'LV0.5';
                            } elseif ($nac['type'] == 'OTP') {
                                $farba = $farby[array_search($nac['type'], $hodnoty2)];
                                $hodn = 'OT0.5';
                            } else {
                                $farba = $farby[array_search($nac['type'], $hodnoty2)];
                                $hodn = $nac['type'];
                            }
                        } else {
                            //$farba = $farby[array_search('OUT', $hodnoty2)];
                            $hodn = '-';
                        }
                    } else {
                        if (jeSviatok(date("U", mktime(0, 0, 0, $datum[0], $i, $datum[1])))) {
                            $farba = 'yellow';
                            $border = '1px solid yellow';
                            $hodn = '';
                        } else {
                            $farba = '#eee';
                            $border = '1px solid black';
                            $hodn = '';
                        }
                    }
                    echo '<td rowspan="1" style="background: ' . $farba . ';height: 20px; width: 30px; border: ' . $border . ';">';
                    echo $hodn;
                    echo '</td>';
                }
                ?>
            </tr>
            <?php
        }
    }
    ?>
</table>
<!-- legenda -->
<table>
    <?php
    for ($l = 0; $l < count($hodnoty3); $l++) {
        echo '<tr style="background: ' . $farby[$l] . '"><td>' . $hodnoty[$l] . '</td><td>' . $hodnoty3[$l] . '</td></tr>';
    }
    ?>
</table>
<!-- koniec legendy -->
<?php
/* $filename ="excelreport.xls";
  header('Content-type: application/ms-excel');
  header('Content-Disposition: attachment; filename='.$filename); */
?>
